package com.example.gokuplayalong.bases.network.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {

    public static final String ENCRYPT_ALGORITHM = "MD5";

    public static String string2MD5(String inSrc) {
        String result = null;
        MessageDigest messagedigest = null;

        try {
            messagedigest = MessageDigest.getInstance(ENCRYPT_ALGORITHM);
            byte[] bytes = inSrc.getBytes();
            byte[] targ = messagedigest.digest(bytes);
            result = bintoascii(targ);
        } catch (NoSuchAlgorithmException nsaex) {
            throw new RuntimeException(nsaex);
        }

        return result;
    }

    private static String bintoascii(byte[] bySourceByte) {
        int len, i;
        byte tb;
        char high, tmp, low;
        String result = "";
        len = bySourceByte.length;
        for (i = 0; i < len; i++) {
            tb = bySourceByte[i];

            tmp = (char) ((tb >>> 4) & 0x000f);
            if (tmp >= 10)
                high = (char) ('a' + tmp - 10);
            else
                high = (char) ('0' + tmp);
            result += high;
            tmp = (char) (tb & 0x000f);
            if (tmp >= 10)
                low = (char) ('a' + tmp - 10);
            else
                low = (char) ('0' + tmp);

            result += low;
        }
        return result;
    }

}

